﻿Imports System.Runtime.CompilerServices
Imports System.Text

''' <summary>
''' DBコマンド拡張メソッド
''' </summary>
''' <remarks>
''' #125 拡張メソッドのモジュール名訂正
''' DBCommandExtension → IDbCommandExtension
''' </remarks>
Public Module IDbCommandExtension

    ''' <summary>
    ''' コマンドロギングイベント
    ''' </summary>
    ''' <remarks>#123 SQLロギングイベント名を変更</remarks>
    <Obsolete("OnLoggingを使用してください（ver0.5.0.3）")>
    Public Event OnDBCommandLogging As EventHandler(Of DBCommandBuiltEventArgs)

    ''' <summary>
    ''' コマンドロギングイベント
    ''' </summary>
    ''' <remarks></remarks>
    Public Event OnLogging As EventHandler(Of DBCommandBuiltEventArgs)

    ''' <summary>
    ''' パラメータを追加します
    ''' </summary>
    ''' <param name="source"></param>
    ''' <param name="name"></param>
    ''' <param name="val"></param>
    ''' <returns></returns>
    ''' <remarks>#123 SQLロギングイベント名を変更</remarks>
    <Extension()>
    Public Function AddParameter(source As IDbCommand, name As String, val As Object) As IDbDataParameter
        If source Is Nothing Then Throw New ArgumentNullException("source")
        If name Is Nothing OrElse String.IsNullOrEmpty(name) Then Throw New ArgumentNullException("name")

        Dim prm = source.CreateParameter

        prm.ParameterName = name
        prm.Value = If(val Is Nothing, DBNull.Value, val)
        source.Parameters.Add(prm)

        Return prm
    End Function

    ''' <summary>
    ''' ログ書き込みします
    ''' </summary>
    ''' <param name="source"></param>
    ''' <returns></returns>
    ''' <remarks>#123 SQLロギングイベント名を変更</remarks>
    <Extension(), Obsolete("Logging、OnLoggingを使用してください（ver0.5.0.3）")>
    Public Function WriteLog(source As IDbCommand) As IDbCommand
        RaiseEvent OnLogging(Nothing, New DBCommandBuiltEventArgs With {.Command = source})
        Return source
    End Function

    ''' <summary>
    ''' ログ書き込みします
    ''' </summary>
    ''' <param name="source"></param>
    ''' <returns></returns>
    ''' <remarks>#123 SQLロギングイベント名を変更</remarks>
    <Extension()>
    Public Function Logging(source As IDbCommand) As IDbCommand
        RaiseEvent OnLogging(Nothing, New DBCommandBuiltEventArgs With {.Command = source})
        Return source
    End Function

    ''' <summary>
    ''' ハードコード文字列変換します
    ''' </summary>
    ''' <param name="source"></param>
    ''' <remarks>ログ用</remarks>
    <Extension()>
    Public Function ToHardCode(source As IDbCommand) As String
        If source Is Nothing Then Throw New ArgumentNullException("source")

        Dim s = source.CommandText
        'ロギング用途のため、見やすさを重視。改行を除去して1クエリ1行にします
        If s.Contains(ControlChars.NewLine) Then
            s = s.Replace(ControlChars.NewLine, " ")
        ElseIf s.Contains(ControlChars.Lf) Then
            s = s.Replace(ControlChars.Lf, " ")
        Else
            s = s.Replace(ControlChars.Cr, " ")
        End If

        For Each item As IDbDataParameter In source.Parameters
            s = s.Replace(item.ParameterName, String.Format("'{0}'", item.Value))
        Next
        Return s.Trim
    End Function
End Module
